package cn.mall.order.service.impl;

import cn.mall.common.constant.MemberResponseVo;
import cn.mall.order.feign.CartFeignService;
import cn.mall.order.feign.MemberFeignService;
import cn.mall.order.vo.OrderConfirmVo;
import cn.mall.order.vo.OrderItemVo;
import io.netty.util.concurrent.CompleteFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.mall.common.utils.PageUtils;
import cn.mall.common.utils.Query;

import cn.mall.order.dao.OrderDao;
import cn.mall.order.entity.OrderEntity;
import cn.mall.order.service.OrderService;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {
    @Autowired
    MemberFeignService memberFeignService;
    @Autowired
    CartFeignService cartFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                new QueryWrapper<OrderEntity>()
        );

        return new PageUtils(page);
    }
    //订单确定要返回的数据在接口orderservice的方法
    @Override
    public OrderConfirmVo confirmOrder(String cartKey){


        OrderConfirmVo confirmVo=new OrderConfirmVo();
        System.out.println(cartKey);
        //因为有登录拦截器所以直接从拦截器里获取登录用户
//        MemberResponseVo memberResponseVo=LoginUserInterceptor.loginUser.get();
        //1.远程查询所有的收货地址列表
//        memberFeignService.getAddress(memberResponseVo.getId());
        //第一个异步任务
//        CompletableFuture<void> getAddressFuture=CompletableFuture.runAsync(()->{
//            List<MemberResponseVo> address=memberFeignService.getAddress(memberResponseVo.getId());
//            confirmVo.getAddress(address);
//        },executor);
        //2.远程查询购物车所有选中的购物项
        //异步调用
//        Executor executor=new Executor() {
//            @Override
//            public void execute(Runnable command) {
//
//            }
//        };
//        CompletableFuture<Void> cartFuture= CompletableFuture.runAsync(()->{
            List<OrderItemVo> items=cartFeignService.getCurrentCartItems(cartKey);
            System.out.println("w--------远程调用购物车信息="+items);
            //购物项数据
            confirmVo.setItems(items);
//        },executor);

        //feign在远程调用之前要构建请求，调用很多的拦截器
        //RequestInterceptor interceptor:requestInterceptors
        //3.查询用户积分,积分的integration是从OrderConfirmVo里面取得的
//        Integer integration=memberResponseVo.getIntegeration();
//        confirmVo.setIntegration(integration);

        return confirmVo;
    }

}
